package core.statistics;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import core.AgentInfo;

/**
 * @author Diego
 * 
 */
public class NStatistics {
	// nome do agente, suas estatisticas uhu
	private Map<String, AgentStatistics> agents = null;
	private List<String> agentsNames = null;
	private int currentRound;

	public NStatistics() {
		this.agents = new HashMap<String, AgentStatistics>();
		this.agentsNames = new LinkedList<String>();
		this.currentRound = 0;
	}

	public NStatistics(List<String> agentNames) {
		this();
		for (String agentsName : agentNames) {
			this.addAgent(agentsName);
			this.agentsNames.add(agentsName);
		}
	}

	public NStatistics(ArrayList<AgentInfo> agentInfoList) {
		this();
		for (AgentInfo agentInfo : agentInfoList) {
			this.getAgents()
					.put(agentInfo.getName(), agentInfo.getStatistics());
			this.agentsNames.add(agentInfo.getName());
		}
	}

	// statistics

	public void printWinningPercentage(String agentsName) {
		if (this.currentRound == 0) {
			System.out.println("Any games were finished yet!");
			return;
		}
		double percentage = ((double) this.getAgents().get(agentsName)
				.getVictoryCount() / this.currentRound) * 100;
		printAgentsName(agentsName);
		System.out.println("*** Winning Percentage: " + percentage + " %\n");
	}

	public void printWinningPercentage() {
		if (this.currentRound == 0) {
			System.out.println("Any games were finished yet!");
			return;
		}

		for (String agentsName : this.agentsNames) {
			printWinningPercentage(agentsName);
		}
	}

	public void printAllPercentages() {
		if (this.currentRound == 0) {
			System.out.println("Any games were finished yet!");
			return;
		}

		double percentage = 0.0;

		// Winning Percentage
		// printWinningPercentage();
		System.out.println("*** All Percentages after " + this.currentRound
				+ " rounds ***\n");
		System.out.println("*** Winning Percentages: ");
		for (String it : this.agentsNames) {
			percentage = ((double) this.getAgents().get(it).getVictoryCount() / this.currentRound) * 100;
			System.out.println("> " + it + ": " + percentage);
		}

		// Loosing Percentage
		// printLoosingPercentage();
		System.out.println("\n*** Loosing Percentages: ");
//		for (String it : this.agentsNames) {
//			percentage = (1 - ((double) this.getAgents().get(it)
//					.getVictoryCount() / this.currentRound)) * 100;
//			System.out.println("> " + it + ": " + percentage);
//		}

		// Thinking Percentage
		// printThinkingTimePercentage();
		System.out.println("\n*** Thinking Time Percentages: ");
		double total = 0.0;
		for (String it : this.agentsNames) {
			total += this.getAgents().get(it).getThinkingTime();
		}
		for (String it : this.agentsNames) {
			percentage = ((double) this.getAgents().get(it).getThinkingTime() / total) * 100;
			System.out.println("> " + it + ": " + percentage);
		}

		// Pass Percentage
		// printPassPercentage();
		System.out.println("\n*** Pass Percentages: ");
		total = 0.0;
		for (String it : this.agentsNames) {
			total += this.getAgents().get(it).getPassCount();
		}
		for (String it : this.agentsNames) {
			percentage = ((double) this.getAgents().get(it).getPassCount() / total) * 100;
			System.out.println("> " + it + ": " + percentage);
		}

		// Points Percentage
		// printPointsPercentage();
		System.out.println("\n*** Points Percentages: ");
		total = 0.0;
		for (String it : this.agentsNames) {
			total += this.getAgents().get(it).getPointsCount();
		}
		for (String it : this.agentsNames) {
			percentage = (((double) this.getAgents().get(it)
					.getPointsCount() / total)) * 100;
			System.out.println("> " + it + ": " + percentage);
		}

	}

	public void printLoosingPercentage(String agentsName) {
		if (this.currentRound == 0) {
			System.out.println("Any games were finished yet!");
			return;
		}

		double percentage = (1 - ((double) this.getAgents().get(agentsName)
				.getVictoryCount() / this.currentRound)) * 100;
		printAgentsName(agentsName);
		System.out.println("*** Loosing Percentage: " + percentage + " %\n");
	}

	public void printLoosingPercentage() {
		if (this.currentRound == 0) {
			System.out.println("Any games were finished yet!");
			return;
		}

		for (String agentsName : this.agentsNames) {
			printLoosingPercentage(agentsName);
		}
	}

	public void printThinkingTimePercentage(String agentsName) {
		if (this.currentRound == 0) {
			System.out.println("Any games were finished yet!");
			return;
		}

		double totalThinkingTime = 0;
		for (String agentsNameIt : this.agentsNames) {
			totalThinkingTime += this.getAgents().get(agentsNameIt)
					.getThinkingTime();
		}

		double percentage = ((double) this.getAgents().get(agentsName)
				.getThinkingTime() / totalThinkingTime) * 100;
		printAgentsName(agentsName);
		System.out.println("*** Thinking Time Percentage: " + percentage
				+ " %\n");
	}

	public void printThinkingTimePercentage() {
		if (this.currentRound == 0) {
			System.out.println("Any games were finished yet!");
			return;
		}

		for (String agentsName : this.agentsNames) {
			printThinkingTimePercentage(agentsName);
		}
	}

	public void printPassPercentage(String agentsName) {
		if (this.currentRound == 0) {
			System.out.println("Any games were finished yet!");
			return;
		}

		double totalPassingCount = 0;
		for (String agentsNameIt : this.agentsNames) {
			totalPassingCount += this.getAgents().get(agentsNameIt)
					.getPassCount();
		}

		double percentage = ((double) this.getAgents().get(agentsName)
				.getPassCount() / totalPassingCount) * 100;
		printAgentsName(agentsName);
		System.out.println("*** Pass Percentage: " + percentage + " %\n");
	}

	public void printPassPercentage() {
		if (this.currentRound == 0) {
			System.out.println("Any games were finished yet!");
			return;
		}

		for (String agentsName : this.agentsNames) {
			printPassPercentage(agentsName);
		}
	}

	public void printPointsPercentage(String agentsName) {
		if (this.currentRound == 0) {
			System.out.println("Any games were finished yet!");
			return;
		}

		double totalPoints = 0;
		for (String agentsNameIt : this.agentsNames) {
			totalPoints += this.getAgents().get(agentsNameIt).getPointsCount();
		}

		double percentage = ((double) this.getAgents().get(agentsName)
				.getPointsCount() / totalPoints) * 100;
		printAgentsName(agentsName);
		System.out.println("Points Percentage: " + percentage + " %\n");
	}

	public void printPointsPercentage() {
		if (this.currentRound == 0) {
			System.out.println("Any games were finished yet!");
			return;
		}

		for (String agentsName : this.agentsNames) {
			printPointsPercentage(agentsName);
		}
	}

	// end of statistics

	public void increaseRound() {
		this.currentRound++;
	}

	public void increaseRound(int moreRoundsPlayed) {
		this.currentRound += moreRoundsPlayed;
	}

	public void setCurrentRound(int totalRoundsPlayed) {
		this.currentRound = totalRoundsPlayed;
	}

	public int getRoundsPlayed() {
		return this.currentRound;
	}

	private void addAgent(String name) {
		AgentStatistics newAgent = new AgentStatistics();
		this.agents.put(name, newAgent);
	}

	private Map<String, AgentStatistics> getAgents() {
		return this.agents;
	}

	private void setPlayers(Map<String, AgentStatistics> newList) {
		this.agents = newList;
	}

	public void addPointsCount(String agent, int p) {
		this.agents.get(agent).addPointsCount(p);
	}

	public void addThinkingTime(String agent, long t) {
		this.agents.get(agent).addThinkingTime(t);
	}

	public int getPassCount(String agent) {
		return this.agents.get(agent).getPassCount();
	}

	public int getPointsCount(String agent) {
		return this.agents.get(agent).getPointsCount();
	}

	public long getThinkingTime(String agent) {
		return this.agents.get(agent).getThinkingTime();
	}

	public int getVictoryCount(String agent) {
		return this.agents.get(agent).getVictoryCount();
	}

	public void increasePassCount(String agent) {
		this.agents.get(agent).increasePassCount();
	}

	public void increaseVictoryCount(String agent) {
		this.agents.get(agent).increaseVictoryCount();
	}

	public void printAllStatistics() {
		for (String agentsName : this.agentsNames) {
			this.printAgentsName(agentsName);
			System.out.println(this.getAgents().get(agentsName).myToString());
		}
	}

	public void printAllThinkingTimeStatistics() {
		for (String agentsName : this.agentsNames) {
			// this.printAgentsName(agentsName);
			// System.out.println(this.getAgents().get(agentsName).getThinkingTime());
			printAgentsThinkingTimeStatistics(agentsName);
		}
	}

	public void printAllPassCountStatistics() {
		for (String agentsName : this.agentsNames) {
			// this.printAgentsName(agentsName);
			// System.out.println(this.getAgents().get(agentsName).getPassCount());
			printAgentsPassCountStatistics(agentsName);
		}
	}

	public void printAllVictoryCountStatistics() {
		for (String agentsName : this.agentsNames) {
			// this.printAgentsName(agentsName);
			// System.out.println(this.getAgents().get(agentsName).getVictoryCount());
			printAgentsVictoryCountStatistics(agentsName);
		}
	}

	public void printAllPointsCountStatistics() {
		for (String agentsName : this.agentsNames) {
			// this.printAgentsName(agentsName);
			// System.out.println(this.getAgents().get(agentsName).getPointsCount());
			printAgentsPointsCountStatistics(agentsName);
		}
	}

	public void printAllAgentsStatistics(String agent) {
		this.printAgentsName(agent);
		System.out.println(this.getAgents().get(agent).myToString());
	}

	public void printAgentsThinkingTimeStatistics(String agent) {
		this.printAgentsName(agent);
		System.out.println("*** Thinking Time: "
				+ this.getAgents().get(agent).getThinkingTime() + "\n");
	}

	public void printAgentsPassCountStatistics(String agent) {
		this.printAgentsName(agent);
		System.out.println("*** Pass: "
				+ this.getAgents().get(agent).getPassCount() + "\n");
	}

	public void printAgentsPointsCountStatistics(String agent) {
		this.printAgentsName(agent);
		System.out.println("*** Points: "
				+ this.getAgents().get(agent).getPointsCount() + "\n");
	}

	public void printAgentsVictoryCountStatistics(String agent) {
		this.printAgentsName(agent);
		System.out.println("*** Victories: "
				+ this.getAgents().get(agent).getVictoryCount() + "\n");
	}

	public void printAgentsName(String name) {
		System.out.println("> Agent " + name + ": ");
	}

}